package com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckDO;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckRectificationFileDO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;

import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.*;
import com.xiaoshuidi.cloud.module.member.convert.securitycheck.SecurityCheckConvert;
import com.xiaoshuidi.cloud.module.member.service.securitycheck.SecurityCheckService;

@Tag(name = "管理后台 - 安全检查")
@RestController
@RequestMapping("/member/security-check")
@Validated
public class SecurityCheckController {

    @Resource
    private SecurityCheckService securityCheckService;

    @PostMapping("/create")
    @Operation(summary = "创建安全检查")
    @PreAuthorize("@ss.hasPermission('member:security-check:create')")
    public CommonResult<Long> createSecurityCheck(@Valid @RequestBody SecurityCheckCreateReqVO createReqVO) {
        return success(securityCheckService.createSecurityCheck(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安全检查")
    @PreAuthorize("@ss.hasPermission('member:security-check:update')")
    public CommonResult<Boolean> updateSecurityCheck(@Valid @RequestBody SecurityCheckUpdateReqVO updateReqVO) {
        securityCheckService.updateSecurityCheck(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安全检查")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:security-check:delete')")
    public CommonResult<Boolean> deleteSecurityCheck(@RequestParam("id") Long id) {
        securityCheckService.deleteSecurityCheck(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安全检查")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:security-check:query')")
    public CommonResult<SecurityCheckRespVO> getSecurityCheck(@RequestParam("id") Long id) {
        SecurityCheckDO securityCheck = securityCheckService.getSecurityCheck(id);
        return success(SecurityCheckConvert.INSTANCE.convert(securityCheck));
    }

    @GetMapping("/list")
    @Operation(summary = "获得安全检查列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:security-check:query')")
    public CommonResult<List<SecurityCheckRespVO>> getSecurityCheckList(@RequestParam("ids") Collection<Long> ids) {
        List<SecurityCheckDO> list = securityCheckService.getSecurityCheckList(ids);
        return success(SecurityCheckConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安全检查分页")
    @PreAuthorize("@ss.hasPermission('member:security-check:query')")
    public CommonResult<PageResult<SecurityCheckRespVO>> getSecurityCheckPage(@Valid SecurityCheckPageReqVO pageVO) {
        return success(securityCheckService.getSecurityCheckPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安全检查 Excel")
    @PreAuthorize("@ss.hasPermission('member:security-check:export')")
    @OperateLog(type = EXPORT)
    public void exportSecurityCheckExcel(@Valid SecurityCheckExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SecurityCheckDO> list = securityCheckService.getSecurityCheckList(exportReqVO);
        // 导出 Excel
        List<SecurityCheckExcelVO> datas = SecurityCheckConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "安全检查.xls", "数据", SecurityCheckExcelVO.class, datas);
    }

    @GetMapping("/pigeonhole")
    @Operation(summary = "归档")
    @Parameter(name = "id", description = "id")
    @PreAuthorize("@ss.hasPermission('member:security-check:update')")
    public CommonResult<Boolean> pigeonhole(Long id) {
        return success(securityCheckService.pigeonhole(id));
    }

    @PostMapping("/upload-rectification-file")
    @Operation(summary = "上传整改文件")
    @PreAuthorize("@ss.hasPermission('member:security-check:create')")
    public CommonResult<Long> uploadRectificationFile(@RequestBody UploadRectificationFileReqVO reqVO){
        String loginUserName = SecurityFrameworkUtils.getLoginUser().getNickname();
        return success(securityCheckService.uploadRectificationFile(loginUserName, reqVO.getSecurityCheckId(), reqVO.getFileUrl(), reqVO.getFileType()));
    }

    @DeleteMapping("/rectification-file-delete")
    @Operation(summary = "删除整改文件")
    @Parameters({
            @Parameter(name = "rectificationFileId", description = "整改文件id"),
            @Parameter(name = "securityCheckId", description = "安全检查id")
    })
    @PreAuthorize("@ss.hasPermission('member:security-check:delete')")
    public CommonResult<Boolean> rectificationFileDelete(Long rectificationFileId, Long securityCheckId){
        return success(securityCheckService.rectificationFileDelete(rectificationFileId, securityCheckId));
    }

    @GetMapping("/rectification-file-page")
    @Operation(summary = "整改文件分页")
    @PreAuthorize("@ss.hasPermission('member:security-check:query')")
    public CommonResult<PageResult<SecurityCheckRectificationFileDO>> rectificationFilePage(Long securityCheckId, Integer pageNo, Integer pageSize){
        return success(securityCheckService.rectificationFilePage(securityCheckId, pageNo, pageSize));
    }



}
